package com.yeebo.admin.service;

import com.yeebo.admin.domain.SysResourceDomain;
import com.yeebo.enums.admin.ResourceLevelEnums;
import com.yeebo.enums.admin.ResourceTypeEnums;
import com.yeebo.model.admin.SysResource;
import com.yeebo.rpc.admin.SysResourceService;
import com.yeebo.vo.admin.SysResourcesTreeVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysResourceServiceImpl implements SysResourceService {

    @Resource
    private SysResourceDomain sysResourceDomain;

    @Override
    public List<SysResourcesTreeVO> loadSysResourceManageList(Integer resourceType, String filterResourceName, Long parentMenuId) {
        if (ResourceTypeEnums.MENU.getType().equals(resourceType)) {
            return sysResourceDomain.loadMenuManageList(filterResourceName);
        } else {
            return sysResourceDomain.loadResourceManageList(parentMenuId, filterResourceName);
        }
    }

    @Override
    public List<SysResourcesTreeVO> getMenuListByUserId(Long userId) {
        return sysResourceDomain.getMenuListByUserId(userId);
    }

    @Override
    public void addMenu(String resourceCode, String resourceName, String resourceDesc, Long parentMenuId, String menuIcon, String menuUrl, Integer sequence) {
        SysResource menuEntity = new SysResource();
        menuEntity.setResourceLevel(parentMenuId == 0 ? ResourceLevelEnums.ONE.getLevel() : ResourceLevelEnums.TWO.getLevel());
        menuEntity.setResourceType(ResourceTypeEnums.MENU.getType());
        constructEntity(menuEntity, resourceCode, resourceName, resourceDesc, parentMenuId, menuIcon, menuUrl, sequence);
        sysResourceDomain.addSysResource(menuEntity);
    }

    @Override
    public void addOperation(String resourceCode, String resourceName, String resourceDesc, Long parentMenuId, String menuIcon, String menuUrl, Integer sequence) {
        SysResource menuEntity = new SysResource();
        menuEntity.setResourceLevel(ResourceLevelEnums.THREE.getLevel());
        menuEntity.setResourceType(ResourceTypeEnums.BUTTON.getType());
        constructEntity(menuEntity, resourceCode, resourceName, resourceDesc, parentMenuId, menuIcon, menuUrl, sequence);
        sysResourceDomain.addSysResource(menuEntity);
    }

    @Override
    public void updateSysResource(Long sysResourceId, String resourceName, String resourceDesc, Long parentMenuId, String menuIcon, String menuUrl, Integer sequence) {
        SysResource existEntity = sysResourceDomain.getSysResourceById(sysResourceId);
        existEntity.setMenuName(resourceName);
        existEntity.setResourceDesc(resourceDesc);
        existEntity.setParentMenuId(parentMenuId);
        existEntity.setMenuIcon(menuIcon);
        existEntity.setMenuUrl(menuUrl);
        existEntity.setSequence(sequence);
        sysResourceDomain.updateSysResource(existEntity);
    }

    @Override
    public List<SysResourcesTreeVO> loadTopLevelMenuList() {
        return sysResourceDomain.loadTopLevelMenuList();
    }

    @Override
    public SysResource getResourceById(Long resourceId) {
        return sysResourceDomain.getSysResourceById(resourceId);
    }

    @Override
    public void deleteSysResourceById(Long sysResourceId) {
        sysResourceDomain.deleteSysResource(sysResourceId);
    }

    private void constructEntity(SysResource menuEntity, String resourceCode, String resourceName, String resourceDesc, Long parentMenuId, String menuIcon, String menuUrl, Integer sequence) {
        Date now = new Date();
        menuEntity.setResourceCode(resourceCode);
        menuEntity.setMenuName(resourceName);
        menuEntity.setResourceDesc(resourceDesc);
        menuEntity.setParentMenuId(parentMenuId);
        menuEntity.setMenuIcon(menuIcon);
        menuEntity.setMenuUrl(menuUrl);
        menuEntity.setSequence(sequence);
        menuEntity.setValid(true);
        menuEntity.setCreatedTime(now);
        menuEntity.setUpdatedTime(now);
    }

}
